<template>
  <div class="app-container">
    <h4 class="form-header h4">基本信息</h4>
    <el-form ref="form" :model="form" label-width="80px">
      <el-row>
        <el-col :span="8" :offset="2">
          <el-form-item label="串口号" prop="serialCode">
            <el-input v-model="form.serialCode" disabled />
          </el-form-item>
        </el-col>
        <el-col :span="8" :offset="2">
          <el-form-item label="采集类别" prop="gatherType">
            <el-select v-model="form.gatherType" disabled>
              <el-option v-for="dict in dict.type.gather_type" :key="dict.value" :label="dict.label"
                :value="dict.value"></el-option>
            </el-select>
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>

    <h4 class="form-header h4">设备类别信息</h4>
    <el-table v-loading="loading" :row-key="getRowKey" @row-click="clickRow" ref="table"
      @selection-change="handleSelectionChange" :data="deviceTypes.slice((pageNum - 1) * pageSize, pageNum * pageSize)">
      <el-table-column label="序号" type="index" align="center">
        <template slot-scope="scope">
          <span>{{ (pageNum - 1) * pageSize + scope.$index + 1 }}</span>
        </template>
      </el-table-column>
      <el-table-column type="selection" :reserve-selection="true" width="55"></el-table-column>
      <el-table-column label="头像" align="center" prop="imageUrl">
        <template slot-scope="scope">
          <div>
            <el-avatar :src="scope.row.imageUrl"></el-avatar>
          </div>
        </template>
      </el-table-column>
      <el-table-column label="类别名称" align="center" prop="name" />
      <el-table-column label="类别编码" align="center" prop="code" />
      <el-table-column label="采集类型" align="center" prop="gatherType">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.gather_type" :value="scope.row.gatherType" />
        </template>
      </el-table-column>
      <el-table-column label="采集时间" align="center" prop="gatherTime" />
      <el-table-column label="状态" align="center" prop="status">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.sys_normal_disable" :value="scope.row.status" />
        </template>
      </el-table-column>
      <el-table-column label="创建时间" align="center" prop="createTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.createTime) }}</span>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total > 0" :total="total" :page.sync="pageNum" :limit.sync="pageSize" />

    <el-form label-width="100px">
      <el-form-item style="text-align: center;margin-left:-120px;margin-top:30px;">
        <el-button type="primary" @click="submitForm()">提交</el-button>
        <el-button @click="close()">返回</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>
  
<script>
import { getAuthDeviceType, updateAuthDeviceType } from "@/api/device/serial";

export default {
  name: "AuthDeviceType",
  dicts: ['sys_normal_disable','gather_type'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 分页信息
      total: 0,
      pageNum: 1,
      pageSize: 10,
      // 选中类型编号
      deviceTypeIds: [],
      // 类型信息
      deviceTypes: [],
      // 串口信息
      form: {}
    };
  },
  created() {
    const serialId = this.$route.params && this.$route.params.serialId;
    if (serialId) {
      this.loading = true;
      getAuthDeviceType(serialId).then((response) => {
        this.form = response.serial;
        this.deviceTypes = response.deviceTypes;
        this.total = this.deviceTypes.length;
        this.$nextTick(() => {
          this.deviceTypes.forEach((row) => {
            if (row.flag) {
              this.$refs.table.toggleRowSelection(row);
            }
          });
        });
        this.loading = false;
      });
    }
  },
  methods: {
    /** 单击选中行数据 */
    clickRow(row) {
      this.$refs.table.toggleRowSelection(row);
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.deviceTypeIds = selection.map((item) => item.id);
    },
    // 保存选中的数据编号
    getRowKey(row) {
      return row.id;
    },
    /** 提交按钮 */
    submitForm() {
      const serialId = this.form.id;
      const deviceTypeIds = this.deviceTypeIds.join(",");
      updateAuthDeviceType({ serialId: serialId, deviceTypeIds: deviceTypeIds }).then((response) => {
        this.$modal.msgSuccess("绑定成功");
        this.close();
      });
    },
    /** 关闭按钮 */
    close() {
      const obj = { path: "/device/serial" };
      this.$tab.closeOpenPage(obj);
    },
  },
};
</script>